﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.Friend
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class FriendListByArea : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            int userID = int.Parse(strUserIdFromCookie);

            string strAreaID = context.Request.QueryString["areaID"];

            context.Response.ContentType = "text/plain";
            context.Response.Write(FriendList(context,userID,strAreaID));
        }
        /// <summary>
        /// 好友列表
        /// </summary>
        /// <param name="userID">当前用户的ID</param>
        /// <param name="strFriendClassID">好友分类(无分类则填string.empty)</param>
        /// <returns>字符串</returns>
        private string FriendList(HttpContext context, int userID, string strAreaClassID)
        {
            List<int?> count;
            StringBuilder sbFrdList = new StringBuilder();
            string strFrdList = context.Cache["user_friendlist_main_ul"].ToString();
            DataTable dtFrdList;
            if (!string.IsNullOrEmpty(strAreaClassID))
            {
                dtFrdList = new BLL.User.Friend().GetFriendListByProvince(1, int.MaxValue, userID, Convert.ToInt32(strAreaClassID), out count).Tables[0];
            }
            else
            {
                dtFrdList = new BLL.User.Friend().GetFriendList(1, int.MaxValue, userID, out count).Tables[0];
            }
            for (int i = 0; i < dtFrdList.Rows.Count; i++)
            {
                //根据用户ID判断用户是否有头像,如果没有头像就返回默认头像地址
                string userPortrait = new Jy123.BLL.User.SPace().DefaultPortrait(Convert.ToInt32(dtFrdList.Rows[i]["FFriend"]));

                string str = strFrdList;
                str = str.Replace("$FriendUserID$", dtFrdList.Rows[i]["FFriend"].ToString());
                str = str.Replace("$FriendUserName$", dtFrdList.Rows[i]["UserName"].ToString());
                str = str.Replace("$FriendPortrait$", userPortrait);
                sbFrdList.Append(str);
            }
            return sbFrdList.ToString();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
